import { Api } from './../providers/api-service';
import { Component } from '@angular/core';
import { Platform, App } from 'ionic-angular';
import { Storage } from '@ionic/storage/dist/storage';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { AppVersion } from '@ionic-native/app-version';
import { Device } from '@ionic-native/device';
import { WelcomePage } from '../pages/welcome/welcome';
import { MyJPushService } from '../providers/plugin-service/jPush-service';
import { TabsPage } from '../pages/tabs/tabs';
import { STORE_HAS_GUIDE_KEY } from '../config/constants';
import { StudyListPage } from '../pages/haokur/study/study-list/study-list';

@Component({
	template: '<ion-nav [root]="rootPage"></ion-nav>'
})
export class MyApp {
	rootPage: any = null;

	constructor(
		public platform: Platform,
		public app: App,
		public statusBar: StatusBar,
		public appVersion: AppVersion,
		public splashScreen: SplashScreen,
		private api: Api,
		public device: Device,
		private myJPushService: MyJPushService,
		public storage: Storage
	) {

		platform.ready().then((readySource) => {
			if (readySource === 'cordova') { // app打开打开
				this.cordovaReady()
			}
			else if (readySource === 'dom') { // 网页浏览器打开
				this.domReady()
			}
		});

	}

	// 仅app里面执行
	cordovaReady() {
		this.statusBar.styleDefault();
		this.splashScreen.hide();
		this.myJPushService.jPushInit();//启动极光推送
		this.api.writeLocalStorage("AppVersion", this.appVersion.getVersionCode());

		/**
		 * 判断平台
		 */
		if (this.platform.is('ios')) {
			document.body.style.marginTop = "20px";
			document.body.style.height = "calc(100% - 20px)";
		}
		this.commonReady()
	}

	// dom执行
	domReady() {
		this.commonReady()
		this.api.writeLocalStorage("AppVersion", "1.0");
	}

	// app或dom通用执行
	commonReady() {

		const DeviceType = this.platform.is('ios') ? 2 : 1;
		const DeviceKey = this.device.uuid || 'sssssssssssssss';

		this.api.writeLocalStorage("DeviceType", DeviceType);
		this.api.writeLocalStorage("DeviceKey", DeviceKey);
		this.api.getImgList();

		this.initEntry()
	}

	// 初始化入口
	initEntry() {
		const HasGuide = this.api.getLocalStorage(STORE_HAS_GUIDE_KEY);
		// this.storage.set('firstIn',1)
		this.rootPage = HasGuide ? TabsPage : WelcomePage
		// this.storage.get('firstIn').then((result) => {
		// 	debugger
		// 	if (result == 2) {
		// 		this.app.getRootNav().push(TabsPage);
		// 	} else {
		// 		this.app.getRootNav().push(LoginPage);
		// 	}
		// });
		// this.rootPage = StudyListPage
		// this.rootPage = StudyDetailPage
		// this.rootPage = StudyCommentMore
	}


}
